<?php

defined('SYSPATH') or die('No direct script access.');

class Model_CtScan extends Kohana_Model {

    public function getImgComment($c_id, $p_id, $ct_id) {

        $sql = "SELECT * FROM img_comment WHERE ct_id=" . $ct_id . " and c_id=" . $c_id . " and p_id=" . $p_id;

        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();

        if (count($query) != 0) {
            $comments = $query;
        } else {
            $comments = null;
        }
        return $comments;
    }
    
    public function clearComment($c_id, $p_id, $ct_id)
    {
        DB::delete("img_comment")
                      ->where('c_id','=', $c_id)
                     ->and_where('p_id', '=',$p_id)
                     ->and_where('ct_id', '=',$ct_id)
                    ->execute();
    }
    
    public function insertImgComment($c_id, $p_id, $ct_id, $id, $x, $y, $w, $h, $comment) {
        $sql = "SELECT * FROM img_comment WHERE ct_id=" . $ct_id . " and c_id=" . $c_id . " and p_id=" . $p_id ." and id=".$id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();

        if (count($query) != 0) {
            DB::update("img_comment")
                     ->set(array('x'=>$x, 'y'=>$y,'w'=>$w, 'h'=>$h, 'comment'=>$comment))                
                     ->where('c_id','=', $c_id)
                     ->and_where('p_id', '=',$p_id)
                     ->and_where('ct_id', '=',$ct_id)
                     ->and_where('id', '=',$id)
                    ->execute();
        } else {
             DB::insert("img_comment", 
                            array('c_id', 'p_id', 'ct_id','id','x', 'y','w', 'h', 'comment'
                 ))->values(array($c_id , $p_id , $ct_id , $id, $x, $y , $w, $h,  $comment))->execute();
        }             
    }

}